package com.itheima.server;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class MainApp {
    private static final Logger LOGGER = LoggerFactory.getLogger(MainApp.class);
    //创建线程池
    private static final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 10, 10, TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(10), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
    public static final Object OBJECT = new Object();

    public static void main(String[] args) {

        try {
            ServerSocket serverSocket = new ServerSocket(8888);
            while (true) {
                System.out.println("等待客户端连接........");
                Socket accept = serverSocket.accept();
                LOGGER.info("[服务器端]监听到有客户端连接.");
                threadPoolExecutor.submit(new ServerThead(accept));
            }
        } catch (IOException e) {
            LOGGER.warn("[服务器端]创建ServerSocket对象失败!");
        }

    }
}
